﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using LTP.Accounts.Bus;

namespace jnxc.Web
{
    public partial class RegCompany : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["reg"] = "yes";
                CKFinder.FileBrowser fileBrowser = new CKFinder.FileBrowser();
                fileBrowser.BasePath = "../ckfinder/"; //设置CKFinder的基路径 
                fileBrowser.SetupCKEditor(ckcontent);                
                Page.DataBind();
            }

        }
        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("index.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (IsValid)
            {
                if (Session["CheckCode"] != null && Session["CheckCode"].ToString().ToLower() == txtcode.Text.Trim().ToLower())
                {
                    string fname = this.txtfname.Text;
                    string yz = "";
                    string zj = "";
                    string sd = "";
                    string pic = "";
                    string fadd = this.txtfadd.Text;
                    string ftel = this.txtftel.Text;
                    string flinker = this.txtflinker.Text;
                    string femail = this.txtfemail.Text;
                    string iserror = "";
                    string fcontent = ckcontent.Text;
                    //营业执照                
                    if (fyz.HasFile)
                    {
                        String name = fyz.FileName;
                        String exp = name.Substring(name.LastIndexOf('.')).ToLower();
                        String filename = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        if (exp == ".jpg" || exp == ".gif" || exp == ".png")
                        {
                            if (fyz.PostedFile.ContentLength <= 50 * 1024 * 1024)
                            {
                                try
                                {
                                    fyz.PostedFile.SaveAs(Server.MapPath("~/factorypics/y" + filename + exp));
                                    yz = "/factorypics/y" + filename + exp;
                                }
                                catch (Exception ex)
                                {
                                    iserror = "营业执照";

                                    ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('营业执照上传文件失败！');", true);
                                }
                            }
                            else
                            {
                                iserror = "营业执照";
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('营业执照文件大于50m');", true);
                            }
                        }
                        else
                        {

                            ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('营业执照文件格式不正确');", true);
                            iserror = "营业执照";
                        }

                    }
                    //组织机构代码证                
                    if (fzj.HasFile)
                    {
                        String name = fyz.FileName;
                        String exp = name.Substring(name.LastIndexOf('.')).ToLower();
                        String filename = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        if (exp == ".jpg" || exp == ".gif" || exp == ".png")
                        {
                            if (fzj.PostedFile.ContentLength <= 50 * 1024 * 1024)
                            {
                                try
                                {
                                    fzj.PostedFile.SaveAs(Server.MapPath("~/factorypics/z" + filename + exp));
                                    zj = "/factorypics/z" + filename + exp;
                                }
                                catch (Exception ex)
                                {
                                    iserror = "组织机构代码证";

                                    ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('组织机构代码证上传文件失败！');", true);
                                }
                            }
                            else
                            {
                                iserror = "组织机构代码证";
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('组织机构代码证文件大于50m');", true);
                            }
                        }
                        else
                        {

                            ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('组织机构代码证文件格式不正确');", true);
                            iserror = "组织机构代码证";
                        }

                    }
                    //税务登记证                
                    if (fsd.HasFile)
                    {
                        String name = fsd.FileName;
                        String exp = name.Substring(name.LastIndexOf('.')).ToLower();
                        String filename = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        if (exp == ".jpg" || exp == ".gif" || exp == ".png")
                        {
                            if (fsd.PostedFile.ContentLength <= 50 * 1024 * 1024)
                            {
                                try
                                {
                                    fsd.PostedFile.SaveAs(Server.MapPath("~/factorypics/s" + filename + exp));
                                    sd = "/factorypics/s" + filename + exp;
                                }
                                catch (Exception ex)
                                {
                                    iserror = "税务登记证";

                                    ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('税务登记证上传文件失败！');", true);
                                }
                            }
                            else
                            {
                                iserror = "税务登记证";
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('税务登记证文件大于50m');", true);
                            }
                        }
                        else
                        {

                            ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('税务登记证文件格式不正确');", true);
                            iserror = "税务登记证";
                        }

                    }
                    //pic                
                    if (fpic.HasFile)
                    {
                        String name = fpic.FileName;
                        String exp = name.Substring(name.LastIndexOf('.')).ToLower();
                        String filename = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        if (exp == ".jpg" || exp == ".gif" || exp == ".png")
                        {
                            if (fpic.PostedFile.ContentLength <= 50 * 1024 * 1024)
                            {
                                try
                                {
                                    fpic.PostedFile.SaveAs(Server.MapPath("~/factorypics/q" + filename + exp));
                                    pic = "/factorypics/q" + filename + exp;
                                }
                                catch (Exception ex)
                                {
                                    iserror = "企业照片";

                                    ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('企业照片上传文件失败！');", true);
                                }
                            }
                            else
                            {
                                iserror = "企业照片";
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('企业照片文件大于50m');", true);
                            }
                        }
                        else
                        {

                            ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('企业照片文件格式不正确');", true);
                            iserror = "企业照片";
                        }

                    }
                    if (iserror == "")
                    {
                        jnxc.Model.factoryinfo model = new jnxc.Model.factoryinfo();

                        model.fname = fname;
                        model.yz = yz;
                        model.zj = zj;
                        model.sd = sd;
                        model.fadd = fadd;
                        model.ftel = ftel;
                        model.flinker = flinker;
                        model.femail = femail;
                        model.fstate = 0;
                        model.fcontent = fcontent;
                        model.fpic = pic;
                        model.ftype = ddltype.SelectedItem.Text;
                        jnxc.BLL.factoryinfo bll = new jnxc.BLL.factoryinfo();
                        bll.Add(model);
                        jnxc.Common.MessageBox.ShowAndRedirect(this, "提交成功！请联系管理员进行审核！", "ecshop/index.aspx");


                    }

                }
                else
                {
                    jnxc.Common.MessageBox.Show(this, "验证码不对！");
                }
                
               
            }
            

        }


    }
}